//
//  Person.m
//  javascriptcore测试
//
//  Created by Tian on 16/4/21.
//  Copyright © 2016年 TianTengFei. All rights reserved.
//

#import "MyJSObject.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <UIKit/UIKit.h>

@protocol PersonJSExport <JSExport>

//- (void)nslog:(NSString *)str;
JSExportAs(showInfoFromJsPlay,
           - (void)printWithsection_imgurl:(NSString *)section_imgurl section_name:(NSString *)section_name stimes:(NSString *)stimes section_videoid:(NSString *)section_videoid courseid:(NSString *)courseid cgrade:(NSString *)cgrade
           );
JSExportAs(showInfoFromJsDownload,
           - (void)printWithsection_imgurl:(NSString *)section_imgurl section_name:(NSString *)section_name esort:(NSString *)esort section_videoid:(NSString *)section_videoid courseid:(NSString *)courseid cgrade:(NSString *)cgrade stimes:(NSString *)stimes
           );
JSExportAs(showInfoFromJsInviteUrl,
           - (void)printWithinviteid:(int )inviteid url:(NSString *)url
           );
JSExportAs(showInfoFromJsBack,
           - (void)printWithStatus1:(NSString*)status1
           );
JSExportAs(showInfoFromJsVip,
           - (void)printWithStr:(int)str
           );
JSExportAs(showInfoFromJsMessage,
           - (void)printWithCollectionStr:(NSString*)str
           );
JSExportAs(showInfoFromJsUserInfo,
           - (void)printWithUserid:(NSString*)userid
           );
@end

@interface MyJSObject() <PersonJSExport>

//- (void)nslog:(NSString *)str;
//- (void)printSome;
@property(strong, nonatomic) UIWebView *webView;

@end

@implementation MyJSObject

- (instancetype)initWithWebView:(UIWebView *)webView {
    if (self = [super init]) {
        _webView = webView;
    }
    return self;
}

+ (instancetype)objectWithWebView:(UIWebView *)webView {
    return [[self alloc] initWithWebView:webView];
}

//- (void)nslog:(NSString *)str {
//    NSLog(@"测试一下JavaScript!");
//}
//
//播放
- (void)printWithsection_imgurl:(NSString *)section_imgurl section_name:(NSString *)section_name stimes:(NSString *)stimes section_videoid:(NSString *)section_videoid courseid:(NSString *)courseid cgrade:(NSString *)cgrade{
    
    NSDictionary *dict = @{@"section_imgurl":section_imgurl,@"section_name":section_name,@"section_videoid":section_videoid,@"courseid":courseid,@"cgrade":cgrade};
    if ([self.delegate respondsToSelector:@selector(MyJSObject:dict:)]) {
        [self.delegate MyJSObject:self dict:dict];
    }
    //[[NSNotificationCenter defaultCenter]postNotificationName:@"playVideo" object:nil userInfo:dict];
//    NSLog(@"播放：%@--%@--%@--%@--%@--%@", section_imgurl,section_name,stimes,section_videoid,courseid,cgrade);
}
//下载
- (void)printWithsection_imgurl:(NSString *)section_imgurl section_name:(NSString *)section_name esort:(NSString *)esort section_videoid:(NSString *)section_videoid courseid:(NSString *)courseid cgrade:(NSString *)cgrade stimes:(NSString *)stimes{
    NSDictionary *dict = @{@"section_imgurl":section_imgurl,@"section_name":section_name,@"section_videoid":section_videoid,@"courseid":courseid,@"cgrade":cgrade,@"stimes":stimes};
    if ([self.delegate respondsToSelector:@selector(MyJSObject:downloadDict:)]) {
        [self.delegate MyJSObject:self downloadDict:dict];
    }
}
- (void)printWithinviteid:(int)inviteid url:(NSString *)url{
    
    NSDictionary *dict = @{@"inviteid":@(inviteid),@"url":url};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"invite" object:nil userInfo:dict];
    
}
- (void)printWithStatus1:(NSString*)status1{
    
    NSDictionary *dict = @{@"status1":status1};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"backAction" object:nil userInfo:dict];
}
- (void)printWithStr:(int)str{
    
    NSDictionary *dict = @{@"str":@(str)};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"openMember" object:nil userInfo:dict];
}
- (void)printWithCollectionStr:(NSString*)str{
    NSDictionary *dict = @{@"str":str};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"collectionControl" object:nil userInfo:dict];
    
}

- (void)printWithUserid:(NSString*)userid{
    NSDictionary *dict = @{@"userid":userid};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pushProfireVC" object:nil userInfo:dict];
    
}
- (void)printWithString:(NSString *)string callback:(NSString *)callback {
//    NSLog(@"%@", string);
    
//    NSString *callbackJS = [NSString stringWithFormat:@"%@('我是callback')", callback];
////    [self.webView stringByEvaluatingJavaScriptFromString:callbackJS];
//    
//    [self.webView performSelector:@selector(stringByEvaluatingJavaScriptFromString:) withObject:callbackJS afterDelay:1];
}

@end
